<template>
  <div class="dashboard-container">
    <div class="app-container">
      <upload-excel
        :before-upload="beforeUpload"
        :on-success="successFn"
      />
    </div>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employess'
import { parseTime } from '@/utils'
import { importEmployee } from '@/api/employees'
const { mapKeyPath } = EmployeeEnum
export default {
  methods: {
    beforeUpload(file) {
      if (!file.name.endsWith('.xlsx')) {
        return this.$message.error('仅支持xlsx格式')
      }
      return true
    },
    async successFn({ header, results }) {
      console.log(header, results)
      const res = results.map(item => {
        const obj = {}
        for (const k in item) {
          if (k === '入职日期' || k === '转正日期') {
            const timesteap = item[k]
            const date = new Date((timesteap - 1) * 24 * 3600000)
            const time = parseTime(date.setFullYear(date.getFullYear() - 70), '{y}-{m}-{d}')
            obj[mapKeyPath[k]] = time
          } else {
            obj[mapKeyPath[k]] = item[k]
          }
        }
        return obj
      })
      await importEmployee(res)
      this.$message.success('导入成功了哦~~')
      this.$router.go(-1)
    }
  }
}
</script>

<style>

</style>
